package xyz.csmcool.imuser.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import xyz.csmcool.imuser.entity.Contact;
import xyz.csmcool.imuser.entity.vo.ContactVo;
import xyz.csmcool.imuser.service.IContactService;
import xyz.csmcool.imutil.utils.ResultTool;
import xyz.csmcool.imutil.webVo.ResultVo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author csmcool
 * @since 2022-08-03
 */
@RestController
@RequestMapping("/contact")
@RequiredArgsConstructor
public class ContactController {

    final IContactService contactService;

    @GetMapping("/get-info")
    public ResultVo<List<Contact>> getContactInfo() {
        List<Contact> list = contactService.list();
        List<Contact> contactList = list.stream().filter(obj -> obj.getContactInfo() != null).collect(Collectors.toList());
        return ResultTool.success(contactList);
    }

    @PutMapping("/admin/update-info")
    public ResultVo updateContactInfo(@Validated @RequestBody ContactVo contactVo) {
        contactService.updateContact(contactVo);
        return ResultTool.success();
    }

}
